﻿$(function(){//延迟加载
	var url='';
	var oltip='';
	var oltId=$('#oltId').val();
	var done=1;//是否编辑结束
	var crows=null;
	$('#desearch').panel({
		iconCls:'icon-search',
		collapsible:true,
		title:'上联OLT查询',
		width:'100%',
		height:'70px'
	});
	
	
	$('#b1').linkbutton({
		iconCls:'icon-add',
		plain:true,
		onClick:function(){
			append();
		}
	});
	$('#b2').linkbutton({
		iconCls:'icon-edit',
		plain:true,
		onClick:function(){
		removeit();
		}
	});
	$('#b3').linkbutton({
		iconCls:'icon-save',
		plain:true,
		onClick:function(){
		accept();
		}
	});
	$('#b4').linkbutton({
		iconCls:'icon-remove',
		plain:true,
		onClick:function(){
		reject();
		}
	});
	$('#onusec').datagrid({
		iconCls: 'icon-edit',
		singleSelect: true,
		toolbar:'#tb',
		url: 'showonulistbyoltId',
		queryParams:{
			oltId:oltId,
			oltip:'',
			onustatus:''
		},
		
		method: 'get',
		title:'ONU信息',
		height:'650px',
		pageSize:20,
		onDblClickRow: onClickRow,
		pagination:true,
		rownumbers:true,
		columns:[[		          		       
		          {field:'onuaddress',width:'10%',title:'安装地址',editor:{
		        	  type:'textbox',
		        		 options:{
		        			 //required:true 
		        		 }
		          }},
		          {field:'onuobd',width:'10%',title:'OBD地址',editor:{
		        	  type:'textbox',
		        		 options:{
		        			 //required:true 
		        		 }
		          }},
		          {field:'onustatus',width:'8%',title:'状态',editor:{
		        	  type:'combobox',
		        	  	 options:{
		        	  		 valueField:'statusvalue',
		        	  		 textField:'status',
		        	  		 data:[{
								statusvalue:'未分配',
								status:'未分配'
		        	  		 	},{
								statusvalue:'预留',
								status:'预留'
		        	  		 	},{
								statusvalue:'在用',
								status:'在用'
							},{
								statusvalue:'故障',
								status:'故障'
							}]
						}
		          }},
		          {field:'pondetail',width:'7%',title:'设备端口',editor:{
		        	  type:'textbox',
		        		 options:{		        			 
		        			 required:true 
		        		 }
		          }},
		          {field:'onupppv',width:'6%',title:'pppoe vlan',editor:{
		        	  type:'numberbox',
		        		 options:{		        			 
		        			 required:true 
		        		 }
		          }},
		          {field:'iptvvlan',width:'6%',title:'iptv vlan',editor:{
		        	  type:'numberbox',
		        		 options:{		        			 
		        			 required:true 
		        		 }
		          }},
		          {field:'ylvlan',width:'6%',title:'预留',editor:{
		        	  type:'numberbox',
		        		 options:{
		        			 required:true 
		        		 }
		          }},
		          {field:'voipvlan',width:'6%',title:'voip vlan',editor:{
		        	  type:'numberbox',
		        		 options:{
		        			 required:true 
		        		 }
		          }},
		          {field:'onuodf',width:'6%',title:'ODF',editor:{
		        	  type:'textbox',
		        		 options:{
		        			 //required:true
		        		 }
		          }},
		          {field:'onutype',width:'5%',title:'PON口类型',editor:{
		        	  type:'textbox',
		        		 options:{
		        			 editable:false
		        		 }
		          }},
		          {field:'onuremark',width:'10%',title:'备注',editor:{
		        	  type:'textbox',
		        		 options:{
		        			 editable:true
		        		 }
		          }},
		          {field:'creatdate',width:'12%',title:'分配时间',editor:'datetimebox',
		        	  formatter:function(value,row,index){
		        		if(value!=null&&value!=''){
		        			var date = new Date(value);
		        			Y = date.getFullYear() + '-';
		        			M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
		        			D = (date.getDate() < 10 ? '0'+(date.getDate()) : date.getDate()) + ' ';
		        			h =(date.getHours() < 10 ? '0'+(date.getHours()) : date.getHours()) + ':';
		        			m =(date.getMinutes() < 10 ? '0'+(date.getMinutes()) : date.getMinutes()) + ':';
		        			s =(date.getSeconds()< 10 ? '0'+(date.getSeconds()) : date.getSeconds()); 
		        			return Y+M+D+h+m+s;
		        		}else{
		        			return '';
		        		}
		        	}}	          
		          ]],
		onLoadSuccess:function(){
//			$(this).datagrid('hideColumn','parentId');
			//这块把DVNAME以及DVBELONG等值传给前台
			//var crows=$('#dg').datagrid('getRows');
			$('#ceshi').linkbutton({
				width:'300px',
				text:'上联OLT：'+$('#onusec').datagrid('getRows')[0].oltname+'('+$('#onusec').datagrid('getRows')[0].oltip+')',
				onClick:function(){
					parent.addTab('上联OLT信息','tooltedit?oltId='+oltId);
				}
			})
		}	
	});
	
	$('#onustatus').combobox({//PON口分配状态
		onChange:function(newValue,oldValue){
			oltip=$('#oltipsec').val();
			oltId=$('#oltsec').combobox('getValue');
			if(oltId==''){
				oltId=$('#oltId').val();
			}
			if(oltId!=''){
				$('#onusec').datagrid('reload',{
					oltId:oltId,
					oltip:'',
					onustatus:newValue
				});
			}else if(oltip!=''){
				$('#onusec').datagrid('reload',{
					oltId:'',
					oltip:oltip,
					onustatus:newValue
				});
			}
		}
	});
	
	$('#sec').load('showjifangsec',function(){
		$.getScript('/xasj/js/component/jifangsec.js');
		$('#juxiang').combobox({
			height:'30px'
		});
		$('#dajuxiang').combobox({
			height:'30px'
		});
		$('#jifang').combobox({
			height:'30px',
			onChange:function(newValue,oldValue){
				if(newValue=='0'){
					$('#oltsec').combobox({
						disabled:true
					});
					$('#oltsec').combobox('clear');
				}else{
					$('#oltsec').combobox('clear');
					$('#oltsec').combobox({
						value:'请选择',
						disabled:false,
						valueField:'oltId',
						textField:'oltname',
						url:"queryoltbyjfId?jfId="+newValue
					});
				}
			}
		});
		//this.append($('#appsec'));
	});
	
	$('#oltipsec').textbox({
		buttonText:'搜索',
		buttonIcon:'icon-search',
		prompt:'OLT IP...',
		onClickButton:function(){
			if($('#oltipsec').val()!='')
			$('#onusec').datagrid('reload',{
				oltId:'',
				oltip:$('#oltipsec').val(),
				onustatus:''
			});
		}
	});
	
	$('#oltsec').combobox({
		height:'30px',
		onChange:function(newValue,oldValue){
			if(newValue!='请选择'){
				//alert(newValue);
				$('#onusec').datagrid('reload',{
					oltId:newValue,
					oltip:'',
					onustatus:''
				});
			}
		}
	
	});
	
	var pager = $('#onusec').datagrid('getPager');	// get the pager of datagrid
	pager.pagination({
		buttons:[{
			iconCls:'icon-ok',
			text:'保存修改',
			width:'200px',
			handler:function(){
				if(done==0){
					alert('请先确认修改');
					return;
				}				
				alert(crows.length);
				if(crows.length==0){
					return;
				}
				
				$.ajax({
					url:'updateonurows',
					type:'POST',
					data:{
						onurows:JSON.stringify(crows)
					},
					success:function(data){
						alert(data.msg);
					}
				});
			}
		},{
			iconCls:'icon-search',
			text:'设备详细拓扑',
			width:'200px',
			handler:function(){				
				alert('tp');
			}
		},{
			iconCls:'icon-redo',
			text:'导出数据',
			width:'200px',
			handler:function(){				
				alert('edit');
			}
		}]
	});
	
	
	var editIndex = undefined;	
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#onusec').datagrid('validateRow', editIndex)){
			//var ed = $('#dg').datagrid('getEditor', {index:editIndex,field:'productid'});
			//var productname = $(ed.target).combobox('getText');
			//$('#dg').datagrid('getRows')[editIndex]['productname'] = productname;
			//$('#dg').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	}

	function onClickRow(index){
		done=0;
		if (editIndex!= index){
			if (endEditing()){
				$('#onusec').datagrid('selectRow', index)
						.datagrid('beginEdit', index);
				editIndex = index;
			} else {
				$('#onusec').datagrid('selectRow', editIndex);
			}
		}
	}

	function append(){
		if (endEditing()){
			var frows=$('#onusec').datagrid('getSelected');
			if(frows==''||frows==null)
			alert('未选中行');
			$('#dg').datagrid('appendRow',
				{
				type:'P',
				dvname:frows.dvname,
				dvip:frows.dvip,
				dvbelong:frows.dvbelong,
				dvId:frows.dvId,
				parentname:frows.parentname,
				parentip:frows.parentip,
				parentbelong:frows.parentbelong,
				parentId:frows.parentId
				});
			editIndex = $('#onusec').datagrid('getRows').length-1;
			$('#onusec').datagrid('selectRow', editIndex)
					.datagrid('beginEdit', editIndex);
			}
		}
	
	function removeit(){
		if (editIndex == undefined){return}
		var drow=$('#onusec').datagrid('getRows')[editIndex];
		//alert(drow.dvrelaId);
		if(drow.dvrelaId==undefined|| drow.dvrelaId==''){			
			return
		}
		$.ajax({
			type:'POST',
			url:'deletedvrelarow',
			data:{
				dvslot:drow.dvslot,
				parentslot:drow.parentslot,
				dvrelaId:drow.dvrelaId
			},
			success:function(data){
				$('#onusec').datagrid('cancelEdit', editIndex)
				.datagrid('deleteRow', editIndex);
				alert(data.msg);				
			}
		});
	}

	function accept(){
		if (endEditing()){
			done=1;
			crows = $('#onusec').datagrid('getChanges','updated');
			$('#onusec').datagrid('acceptChanges');
			editIndex = undefined;
		}
	}

	function reject(){
		$('#onusec').datagrid('rejectChanges');
		editIndex = undefined;
	}

});